package com.itheima.arraylist;

import com.itheima.domain.Student;

import java.util.ArrayList;

public class Test05 {
    public static void main(String[] args) {
        //1.创建集合   泛型 Student
        ArrayList<Student> list = new ArrayList<>();
        //2.创建三个学生对象
        Student wu = new Student("吴亦凡", 16);
        Student zhang = new Student("张艺兴", 15);
        Student huang = new Student("黄子韬", 29);
        Student lu = new Student("鹿晗", 31);
        //添加学生元素
        list.add(wu);
        list.add(zhang);
        list.add(huang);
        list.add(lu);
        //调用方法,返回存储对象为年龄小于18的Student类的新集合
        ArrayList<Student> filter = filter(list);
        //输出新集合
        System.out.println(filter);
    }

    private static ArrayList<Student> filter(ArrayList<Student> list) {
        //创建新的集合
        ArrayList<Student> newList = new ArrayList<>();
        //遍历传入的集合
        for (int i = 0; i < list.size(); i++) {
            //list.get(i)
            //获取遍历集合对象
            Student student = list.get(i);
            //同过get方法获得遍历当前学生对象的年龄
            int age = student.getAge();
            //判断年龄是否小于18
            if (age <= 18) {
                //年龄小于18传入新的集合
                newList.add(student);
            }
        }
        //返回新的集合
        return newList;
    }
}
